package com.jh.paymentchannel.helipay.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.jh.paymentchannel.pojo.HLRegister;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jh.paymentchannel.business.RegisterAuthBusiness;

import cn.jh.common.utils.CommonConstants;
@Controller
@EnableAutoConfiguration
public class HLRegisterService {
	
	private static final Logger LOG = LoggerFactory.getLogger(HLRegisterService.class);
	
	@Autowired
	private RegisterAuthBusiness registerAuthBusiness;
	
	/**
	 * 添加一条HLRegister记录
	 * @param request
	 * @param userId
	 * @param phone
	 * @param name
	 * @param cardNo
	 * @param bankCard
	 * @param bankCardYear
	 * @param bankCardMonth
	 * @param cvv
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/v1.0/paymentchannel/hlregister/add")
	public @ResponseBody Object addHLRegister(HttpServletRequest request,
			@RequestParam(value = "userId") String userId,
			@RequestParam(value = "phone",required = false) String phone,
			@RequestParam(value = "bankName",required = false) String bankName,
			@RequestParam(value = "name",required = false) String name,
			@RequestParam(value = "cardNo",required = false) String cardNo,
			@RequestParam(value = "bindId",required = false) String bindId,
			@RequestParam(value = "cardType",required = false) String cardType,
			@RequestParam(value = "bankCard") String bankCard,
			@RequestParam(value = "bankCardYear",required = false) String bankCardYear,
			@RequestParam(value = "bankCardMonth",required = false) String bankCardMonth,
			@RequestParam(value = "cvv",required = false) String cvv,
			@RequestParam(value = "avaliable",required = false) String avaliable,
			@RequestParam(value = "recieptDay",required = false) String recieptDay,
			@RequestParam(value = "paymentDay",required = false) String paymentDay,
			@RequestParam(value = "status",required = false) String status,
			@RequestParam(value = "balance",required = false) String balance,
			@RequestParam(value = "loginPhone",required = false) String loginPhone,
			@RequestParam(value = "frozenBalance",required = false) String frozenBalance,
			@ModelAttribute HLRegister model) {
		model.setCreateTime(new Date());
		Map map = new HashMap();
//		HLRegister hlRegister = registerAuthBusiness.findHLRegisterByUserId(userId);
		HLRegister hlRegister = null;
		try {
			hlRegister = registerAuthBusiness.findHLRegisterByBankCard(bankCard);
		} catch (Exception e) {
			LOG.error(e.toString() + e.getMessage());
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE,"查询数据异常");
			return map;
		}
		if(hlRegister != null){
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE,"添加失败,表中已有该卡记录");
		}else{
			map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
			map.put(CommonConstants.RESULT, registerAuthBusiness.save(model));
			map.put(CommonConstants.RESP_MESSAGE,"添加成功");
		}
		return map;
	}
	
	/**
	 * 根据userId查询记录
	 * @param request
	 * @param userId
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/v1.0/paymentchannel/hlregister/findbycard")
	public @ResponseBody Object findSFAuthByPhone(
			@RequestParam(value = "bankCard", required=false,defaultValue = "") String bankCard,
			@RequestParam(value = "bindId", required=false,defaultValue = "") String bindId,
			@RequestParam(value = "userId", required=false,defaultValue = "") String userId
			) {
		Map map = new HashMap();
		if("".equals(bankCard)&&"".equals(bindId)&&"".equals(userId)) {
			map.put(CommonConstants.RESP_CODE, CommonConstants.BINDTASKCARD);
			map.put(CommonConstants.RESP_MESSAGE,"参数全为空");
			return map;
		}
//		HLRegister hlRegister = registerAuthBusiness.findHLRegisterByUserId(card);
		HLRegister hlRegister = null;
		List<HLRegister> models = null;
		try {
			if(!"".equals(bankCard)&&("".equals(bindId)&&"".equals(userId))){
				hlRegister = registerAuthBusiness.findHLRegisterByBankCard(bankCard);
			}
			else if(!"".equals(bindId)&&("".equals(bankCard)&&"".equals(userId))){
				hlRegister = registerAuthBusiness.findByBindId(bindId);
			}
			else if(!"".equals(bindId)&&!"".equals(bankCard)&&"".equals(userId)){
				hlRegister = registerAuthBusiness.findHLRegisterByBindIdAndBankCard(bindId,bankCard);
			}else if(!"".equals(userId)&&"".equals(bankCard)&&"".equals(bindId)){
				models = registerAuthBusiness.findByUserId(userId);
			}else{
				map.put(CommonConstants.RESP_CODE, CommonConstants.ERROR_PARAM);
				map.put(CommonConstants.RESP_MESSAGE,"参数异常");
				return map;
			}
			if (hlRegister==null && models==null) {
				map.put(CommonConstants.RESP_CODE, CommonConstants.BINDTASKCARD);
				map.put(CommonConstants.RESP_MESSAGE, "查询不到该条数据");
				return map;
			}
			if(null!=hlRegister){
				map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
				map.put(CommonConstants.RESP_MESSAGE, "查询成功");
				map.put(CommonConstants.RESULT, hlRegister);
			}else{
				map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
				map.put(CommonConstants.RESP_MESSAGE, "查询成功");
				map.put(CommonConstants.RESULT, models);
			}
				
			
		} catch (Exception e) {
			LOG.error("查询异常================" + e);
			map.put(CommonConstants.RESP_CODE, CommonConstants.BINDTASKCARD);
			map.put(CommonConstants.RESP_MESSAGE,"查询数据异常");
			return map;
		}
		
		return map;
	}
	
	
	@RequestMapping(method = RequestMethod.POST, value = "/v1.0/paymentchannel/hlregister/update")
	public @ResponseBody Object updateExemption(HttpServletRequest request,
			@RequestParam(value = "bindId")String bindId,
			@RequestParam(value = "cardType",required = false)String cardType,
			@RequestParam(value = "status",required = false)String status,
			@RequestParam(value = "balance",required = false)String balance,
			@RequestParam(value = "frozenBalance",required = false)String frozenBalance,
			@RequestParam(value = "exemption",required = false)String exemption
			){
		Map map = new HashMap();
		HLRegister hlRegister = null;
		try {
			hlRegister = registerAuthBusiness.findByBindId(bindId);
		} catch (Exception e) {
			LOG.error(e.toString() + e.getMessage());
			map.put(CommonConstants.RESP_CODE, CommonConstants.ERROR_PARAM);
			map.put(CommonConstants.RESP_MESSAGE, "查询数据异常");
			return map;
		}
		if(hlRegister==null){
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "根据bindId查询不到该条数据,修改失败");
			return map;
		}else{
			if(cardType!=null&&!cardType.equals(""))
			hlRegister.setCardType(cardType);
			
			if(status!=null&&!status.equals(""))
			hlRegister.setStatus(status);
			
			if(balance!=null&&!balance.equals(""))
			hlRegister.setBalance(balance);
			
			if(frozenBalance!=null&&!frozenBalance.equals(""))
			hlRegister.setFrozenBalance(frozenBalance);
			
			if(exemption!=null&&!exemption.equals(""))
			hlRegister.setExemption(exemption);
			try {
				registerAuthBusiness.save(hlRegister);
			} catch (Exception e) {
				LOG.error(e.toString() + e.getMessage());
				map.put(CommonConstants.RESP_CODE, CommonConstants.ERROR_PARAM);
				map.put(CommonConstants.RESP_MESSAGE, "更新数据异常");
				return map;
			}
		}
		map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
		map.put(CommonConstants.RESP_MESSAGE, "修改成功");
		map.put(CommonConstants.RESULT, hlRegister);
		return map;
	}
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/hlregister/update/limitimes")
	public @ResponseBody Object updateLimitimesByBindId(HttpServletRequest request,
			@RequestParam(name="bindId")String bindId,
			@RequestParam(name="limitimes")String limitimes
			){
		Map map = new HashMap();
		HLRegister hlRegister = null;
		try {
			hlRegister = registerAuthBusiness.findByBindId(bindId);
		} catch (Exception e) {
			LOG.error(e.getMessage());
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "查询数据异常");
			return map;
		}
		if(hlRegister==null){
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "无此bindId记录");
			return map;
		}
		hlRegister.setLimitimes(limitimes);
		try {
			registerAuthBusiness.save(hlRegister);
		} catch (Exception e) {
			LOG.error(e.getMessage());
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "保存数据异常");
			return map;
		}
		map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
		map.put(CommonConstants.RESP_MESSAGE, "修改成功");
		return map;
	}
	

	@RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/hlregister/update/bybankcard")
	public @ResponseBody Object updateByBankCard(HttpServletRequest request,
			@RequestParam(name="bankCard")String bankCard,
			@RequestParam(name="avaliable" ,defaultValue="",required=false)String avaliable,
			@RequestParam(name="recieptDay",defaultValue="",required=false)String recieptDay,
			@RequestParam(name="paymentDay",defaultValue="",required=false)String paymentDay
			){
		Map map = new HashMap();
		HLRegister model = null;
		try {
			model = registerAuthBusiness.findHLRegisterByBankCard(bankCard);
		} catch (Exception e) {
			LOG.error(e.getMessage());
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "亲,查询出错了哦");
			return map;
		}
		
		if(model==null){
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "亲,找不到要修改的记录哦");
			return map;
		}
		
		model.setAvaliable("".equals(avaliable)?model.getAvaliable():avaliable);
		model.setRecieptDay("".equals(recieptDay)?model.getRecieptDay():recieptDay);
		model.setPaymentDay("".equals(paymentDay)?model.getPaymentDay():paymentDay);
		
		try {
			model = (HLRegister) registerAuthBusiness.save(model);
		} catch (Exception e) {
			LOG.error(e.getMessage());
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "亲,保存出错了哦");
			return map;
		}
		map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
		map.put(CommonConstants.RESP_MESSAGE, "修改成功");
		map.put(CommonConstants.RESULT, model);
		return map;
	}
	

	
}
